package com.qfedu.dao.impl;

import com.qfedu.dao.AccountDao;
import com.qfedu.pojo.Account;
import com.qfedu.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AccountDaoImpl extends BaseDao implements AccountDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public Account queryAccountByPhone(String phone) {
        String sql = "select * from bank_account where phone = ?";
        return queryForOne(Account.class, sql, phone);
    }

    @Override
    public Account queryAccountByIdcode(String idcode) {
        String sql = "select * from bank_account where idcode = ?";
        return queryForOne(Account.class, sql, idcode);
    }

    @Override
    public int saveAccount(Account account) {
        String sql = "insert into bank_account values(?,?,?,?,?)";
        return update(sql, account.getPhone(), account.getIdcode(), account.getName(), account.getPaypassword(), 0);
    }

    @Override
    public Account findAccountByIdCode(String cardnumber) {
        String sql = "select b.* from bank_account b,(select idcode from bank_cards where cardnumber=?) i where b.idcode=i.idcode";
        return queryForOne(Account.class, sql, cardnumber);
    }

    @Override
    public int outMoney(Account source, BigDecimal money, Connection connection) {
        String sql = "update bank_account set money=? where idcode=?";
        return update(sql, money, source.getIdcode());
    }

    @Override
    public int inMoney(Account target, BigDecimal money, Connection connection) {
        String sql = "update bank_account set money=? where idcode=?";
        return update(sql, money, target.getIdcode());
    }

    @Override
    public Account password(String idcode, String password) {
        String sql = "select idcode,paypassword from bank_account where idcode=? and paypassword=?";
        return queryForOne(Account.class, sql, idcode, password);
    }

    @Override
    public Account forgetCheck(String name, String phone, String idcode) {
        String sql="select * from bank_account where name=? and idcode=? and phone=?";
        return queryForOne(Account.class, sql, name,idcode, phone);
    }

    @Override
    public Account queryIdcodeByPhone(String phone) {
        String sql = "select * from bank_account where phone = ?";
        return queryForOne(Account.class,sql,phone);
    }

    @Override
    public void updateMoney(Connection connection ,String cardnumber, BigDecimal mon) {
        String sql = "update bank_account set money=? where idcode = ?";
        update(connection,sql,mon,cardnumber);
    }



    /*----------------------------------------------------------------------------*/



    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());

    @Override
    public Account findAccountIdcode(String s) {
        String sql = "select *from bank_account where idcode = ?";
        List<Account> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class), s);
        if (query != null && query.size() == 1) {
            return query.get(0);
        } else {
            return null;
        }
    }

    @Override
    public void outMoney(Account source, int money, Connection connection) {
        String sql = "update bank_account set money = money-? where phone =?";
        try {
            update(connection, sql, money, source.getPhone());
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }


    @Override
    public Account findMoney() {
        String sql = "select *from bank_account";
        List<Account> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class));
        return query.get(0);
    }

    @Override
    public Account find1(Account account2) {
        String sql = "SELECT *FROM bank_account WHERE idcode =? AND paypassword = ?";
        List<Account> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class), account2.getIdcode(), account2.getPaypassword());
        if (query != null && query.size() == 1) {
            return query.get(0);
        } else {
            return null;
        }
    }

    @Override
    public Account findAccountPhone(String s) {
        String sql = "select *from bank_account where phone = ?";
        List<Account> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class), s);
        if (query != null && query.size() == 1) {
            return query.get(0);
        } else {
            return null;
        }
    }

    @Override
    public void inMoney2(Account target, double money, Connection connection) {
        String sql = "update bank_account set money = money+? where phone =?";
        try {
            queryRunner.update(connection, sql, money, target.getPhone());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public Account findPassword() {
        String sql = "select *from bank_account";
        List<Account> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class));
        return query.get(0);
    }

    @Override
    public int updateMoney(Account account ,BigDecimal add) {
        String sql = "update bank_account set money = ? where phone = ?";
        return update(sql,add,account.getPhone());
    }
}


/*public class AccountDaoImpl extends BaseDao implements AccountDao {
    @Override
    public Account findAccountByIdCode(String cardnumber){
        String sql="select b.* from bank_account b,(select idcode from bank_cards where cardnumber=?) i where b.idcode=i.idcode";
        return queryForOne(Account.class,sql,cardnumber);
    }

    @Override
    public int outMoney(Account source, BigDecimal money, Connection connection) {
        String sql="update bank_account set money=? where idcode=?";
        return update(sql,money,source.getIdcode());
    }

    @Override
    public int inMoney(Account target, BigDecimal money, Connection connection) {
        String sql="update bank_account set money=? where idcode=?";
        return update(sql,money,target.getIdcode());
    }

    @Override
    public Account password(String idcode, String password) {
        String sql="select idcode,paypassword from bank_account where idcode=? and paypassword=?";
        return queryForOne(Account.class,sql,idcode,password);
>>>>>>> Stashed changes
    }*/


